(function(){
    angular.module('app').controller("dspInfoCtrl",function($scope,proxy) {
        $scope.permission = PERMISSION;
        $scope.finance = dspFinance;
        $scope.filter = {
            name:""
        }
        // 转账类型
        $scope.financeType = "";

        $scope.search = function(){
            getList();
        };
      
        // 显示转账弹框
        $scope.showMask = function(type,item) {
            $scope.financeType = type;
            $scope.agencyID = item.agencyID;
            $scope.agentName = item.agencyName;
            $scope.balance = item.balance;
            layer.open({
                type: 1,
                title: type == "putin" ?"充值":"转出资金",
                content: $('.transfer'),
                area:["580px","330px"]
            })
        }

        //提示信息显示
        $scope.tips = function (event) {
            layer.tips("暂无信息", $(event.target), {
                time: 0,
                tips: [1, '#fffee3'] //还可配置颜色
            });
        }

        // 转账
        $scope.transfer = function() {
            var str,url;
            if($scope.financeType == "putin") {
                str = "充值";
                url = STATIC_URL.domian+'/dsp/admin/agencyfinance/deposit';
            } else {
                str = "转出资金";
                url = STATIC_URL.domian+'/dsp/admin/agencyfinance/withdraw';
            }
            layer.confirm('是否确定'+str+'？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                var data = {
                    'agenID': $scope.agencyID,//广告主ID
                    'money': $scope.money,
                };
                proxy.proxy({
                    url: url,
                    load: true,
                    data: {
                        data:data
                    },
                    success: function(){
                        layer.closeAll();
                        layer.msg(str+"成功")
                        getList($("#pageToolbar .focus").html());
                        getFinance();
                    }
                })
            });
        }
        // 下载报表
        $scope.download = function() {
            var data = {
                'page': "1",
                'size': "99999",
                "sort": ["cTime,1"],
                'filter': $scope.filter
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/saas/dsp/finace/agency',
                load: true,
                data: data,
                success: function(res){

                    exportCsv({
                        title: ['广告主名称','现金余额','总竞价交易利润','竞价交易未提取利润','竞价交易已提取利润','总保价保量收入','保价保量未提取收入','保价保量已提取收入'],
                        titleForKey: ['name','balance','rtbProfit','unExtractRtbProfit','extractRtbProfit','pdbProfit','unExtractPdbProfit','extractPdbProfit'],
                        data: res.data.data,
                    },"财务信息.csv");
                }
            })
        }

        getList();
        getFinance();

        function getList(current, pagesize) {
            var data = {
                'page': (current || 1)+"",
                'size': (pagesize || 20)+"",
                "sort": ["cTime,1"],
                'filter': $scope.filter
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/saas/dsp/finace/agency',
                load: true,
                data: data,
                success: function(res){
                    // 获得列表
                    $scope.list = res.data.data;
                    $("#pageToolbar").html("");
                    $('#pageToolbar').Paging({
                        pagesize: pagesize || '20',
                        current: current || '1',
                        count:res.data.total,
                        toolbar:true,
                        'hash': false,
                        callback:getList
                    });
                }
            })
        }
        function getFinance() {
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/saas/dsp/finace/summary',
                data: {},
                success: function(res){
                    $scope.finance = res.data;
                }
            })
        }

    })
})();